package com.org.myapplication.Manager;

import android.content.Context;
import android.os.ServiceManager;
import android.util.Log;

import com.org.af.IAFV2AidlInterface;


public class MagicServiceManager {
    private static MagicServiceManager instance;
    public IAFV2AidlInterface myservice;
    private Context mcontext;
    private final static String TAG = "DLServiceManager";

    public static MagicServiceManager getInstance(Context context) {

        if (instance == null) {
            instance = new MagicServiceManager(context);
        }
        return instance;
    }

    public MagicServiceManager(Context context) {
        this.mcontext = context;
        while (myservice == null) {
            myservice = IAFV2AidlInterface.Stub.asInterface(ServiceManager.getService("MagicAFService"));
            Log.e(TAG, "############[MagicServiceManager][getMyservice][服务是空的]");
        }
        if (myservice == null) {
            Log.e(TAG, "############[MagicServiceManager][getMyservice][服务是空的]");
        } else {
            Log.i(TAG, "############[MagicServiceManager][getMyservice][服务已经拿到]");
        }
    }


    /***
     *设置晃动对焦开关
     *@bl 1:     开
     *@bl 0:     关
     **/
    public void setAfSensor(int status) {
        try {
            Log.i(TAG, "============发送命令：[setAfSensor] parameter = " + status);
            myservice.setAfSensor(status);
        } catch (Exception e) {
            Log.e(TAG, "[setAfSensor]:" + e.getMessage());
        }
    }

    public int getAfSensor() {
        int res = 0;
        try {
            res = myservice.getAfSensor();
            Log.i(TAG, "============发送命令：[getAfSensor] res = " + res);
        } catch (Exception e) {
            Log.e(TAG, "[getAfSensor]:" + e.getMessage());
        }
        return res;
    }

    /***
     *模拟遥控器对焦按键单机执行一次
     **/
    public void do_auto_once() {
        try {
            Log.i(TAG, "============发送命令：[do_auto_once]");
            myservice.do_auto_once();
        } catch (Exception e) {
            Log.e(TAG, "[do_auto_once]:" + e.getMessage());
        }
    }

    /***
     *手动幕布识别一次（供工厂测试apk使用）
     **/
    public void manual_dis_screen_once() {
        try {
            Log.i(TAG, "============发送命令：[manual_dis_screen_once]");
            myservice.manual_dis_screen_once();
        } catch (Exception e) {
            Log.e(TAG, "[manual_dis_screen_once]:" + e.getMessage());
        }
    }

    /***
     *设置自动对焦的状态
     *@status 1      打开
     *@status 0     关闭
     */
    public void setAutoFocus(int status) {
        try {
            Log.i(TAG, "============发送命令：[setAutoFocus] parameter = " + status);
            myservice.setAutoFocus(status);
        } catch (Exception e) {
            Log.e(TAG, "[setAutoFocus]:" + e.getMessage());
        }
    }

    public int getAutoFocus() {
        int res = 0;
        try {
            res = myservice.getAutoFocus();
            Log.i(TAG, "============发送命令：[getAutoFocus] res = " + res);
        } catch (Exception e) {
            Log.e(TAG, "[getAutoFocus]:" + e.getMessage());
        }
        return res;
    }

    /***
     *设置自动六向梯形矫正的状态
     *@status 1      打开
     *@status 0     关闭
     */
    public void setAutoKeyStone(int status) {
        try {
            Log.i(TAG, "============发送命令：[setAutoKeyStone] parameter = " + status);
            myservice.setAutoKeyStone(status);
        } catch (Exception e) {
            Log.e(TAG, "[setAutoKeyStone]:" + e.getMessage());
        }
    }

    public int getAutoKeyStone() {
        int res = 0;
        try {
            res = myservice.getAutoKeyStone();
            Log.i(TAG, "============发送命令：[getAutoKeyStone] res = " + res);
        } catch (Exception e) {
            Log.e(TAG, "[getAutoKeyStone]:" + e.getMessage());
        }
        return res;
    }

    /***
     *设置自动幕布识别的状态
     *@status 1      打开
     *@status 0     关闭
     */
    public void setAutoScreen(int status) {
        try {
            Log.i(TAG, "============发送命令：[setAutoScreen] parameter = " + status);
            myservice.setAutoScreen(status);
        } catch (Exception e) {
            Log.e(TAG, "[setAutoScreen]:" + e.getMessage());
        }
    }

    public int getAutoScreen() {
        int res = 0;
        try {
            res = myservice.getAutoScreen();
            Log.i(TAG, "============发送命令：[getAutoScreen] res = " + res);
        } catch (Exception e) {
            Log.e(TAG, "[getAutoScreen]:" + e.getMessage());
        }
        return res;
    }

    /***
     *设置自动避障的状态
     *@status 1      打开
     *@status 0     关闭
     */
    public void setAutoObstacleavoidance(int status) {
        try {
            Log.i(TAG, "============发送命令：[setAutoObstacleavoidance] parameter = " + status);
            myservice.setAutoObstacleavoidance(status);
        } catch (Exception e) {
            Log.e(TAG, "[setAutoObstacleavoidance]:" + e.getMessage());
        }
    }

    public int getAutoObstacleavoidance() {
        int res = 0;
        try {
            res = myservice.getAutoObstacleavoidance();
            Log.i(TAG, "============发送命令：[getAutoObstacleavoidance] res = " + res);
        } catch (Exception e) {
            Log.e(TAG, "[getAutoObstacleavoidance]:" + e.getMessage());
        }
        return res;
    }


    /****
     *获取当前算法状态码
     **/
    public int get_Algorithm_code() {
        int res = 0;
        try {
            res = myservice.get_Algorithm_code();
            Log.i(TAG, "============发送命令：[get_Algorithm_code] res = " + res);
        } catch (Exception e) {
            Log.e(TAG, "[get_Algorithm_code]:" + e.getMessage());
        }
        return res;
    }

}
